/**
 *t访问网络
 */
var util = require('./util.js');
var app = getApp();
//一般访问网络
function request(url, urltype, method, data, token, message, successBack, failBack) {
    if (message != '') {
        uni.showLoading({
            mask: true,
            title: message
        });
    }
    var atempurl = app.globalData.baseUrl + url;
    //console.log('url++++++');
    //console.log(url);
    //console.log(urltype);
    //console.log(atempurl);
    //console.log(data);
    //console.log(token);
    //console.log('url++++++');
    var aheadertoken = '';
    if (token) {
        aheadertoken = token;
    } else {
        aheadertoken = 'test1';
    }
    //console.log('token++++++');
    //console.log(aheadertoken);
    //console.log('token++++++');

    uni.request({
        url: atempurl,
        data: data,
        header: {
            'tenant-id': app.globalData.tenantId,
            'Content-Type': 'application/json',
            Authorization: 'Bearer ' + aheadertoken
        },
        method: method,
        success: function (res) {
            if (res.statusCode == 200) {
                if (res.data.code == 401) {
                    //直接到登录界面
                    uni.navigateTo({
                        url: '../login/login'
                    });
                } else {
                    successBack(res.data);
                }
            } else if (res.statusCode == 401) {
                //直接到登录界面
                uni.navigateTo({
                    url: '../login/login'
                });
            } else {
                if (res.errMsg != null) {
                    failBack(res.data);
                }
            }
            if (message != '') {
                setTimeout(function () {
                    uni.hideLoading();
                }, 500);
            }
        },
        fail: function (err) {
            if (message != '') {
                setTimeout(function () {
                    uni.hideLoading();
                }, 500);
            }
            failBack(err);
        }
    });
}
/*
微信一键登录
*/
function getLogin(phonecode, loginCode, successBack, failBack) {
    let that = this;
    uni.login({
        success: function (res) {
            //console.log('++++==');
            //console.log(res);
            //console.log('++++==');
            if (res.code != null) {
                that.request(
                    '/member/auth/weixin-mini-app-login',
                    '1',
                    'post',
                    {
                        phoneCode: phonecode,
                        loginCode: loginCode
                    },
                    '登录中...',
                    function success(info) {
                        console.info('返回111===');
                        console.info(info);
                        if (info.code == 1) {
                            //赋值给全局
                            app.globalData.userData = info.data;
                            app.globalData.isLogin = true;
                            //console.log('456+++++++');
                            //console.log(app.globalData.userData);
                            //console.log('456+++++++');
                            //缓存服务器返回的用户信息
                            uni.setStorageSync('userDatatoken', info.data);
                            successBack();
                        } else {
                            uni.showToast({
                                title: info.msg
                            });
                        }
                    },
                    function fail(info) {
                        failBack();
                    }
                );
            } else {
                //console.log('登录失败！' + res.errMsg)
            }
        }
    });
}
/**
 *
 *提问上传文件
 */
function uploadFile(url, data, message, success, fail) {
    uni.showNavigationBarLoading();
    if (message != '') {
        uni.showLoading({
            title: message
        });
    }
    uni.uploadFile({
        url: app.globalData.baseUrl + url,
        //仅为示例，非真实的接口地址
        filePath: data.temFile,
        name: data.file,
        formData: {
            token: app.globalData.userData.token
        },
        header: {
            'Content-Type': 'multipart/form-data'
        },
        success: function (res) {
            uni.hideNavigationBarLoading();
            if (res.statusCode == 200) {
                success(res.data);
            } else {
                fail(res);
            }
        },
        fail: function (err) {
            uni.hideNavigationBarLoading();
            if (message != '') {
                uni.hideLoading();
            }
            fail(err);
        }
    });
}
module.exports = {
    request: request,
    getLogin: getLogin,
    uploadFile: uploadFile
};
